package com.youi.web;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.youi.exception.BusinessException;
import com.youi.manage.HttpRequestManage;
import com.youi.common.DelStatus;
import com.youi.service.UserwechatService;
import com.youi.entity.Userwechat;
import com.youi.common.Common;
import com.youi.util.CommonInfoUtil;
import com.youi.util.DateUtil;
import com.youi.util.JsonUtil;
import com.youi.util.ObjectUtil;
import com.youi.util.Page;

/**
* 用户微信信息控制层
* @author Administrator
*/
@Controller
public class UserwechatController  extends HttpRequestManage{

		@Autowired
		private UserwechatService userwechatService;

		/**
		 *添加或编辑用户微信信息
		*/
		@ResponseBody
		@RequestMapping("addOrEditUserwechat")
		public String addOrEditUserwechat(ModelMap modelMap,HttpServletRequest request)throws BusinessException,ParseException{
			Long id=optLong("id",request);
			Userwechat userwechat=null;
			if (id!=null) {
				userwechat=userwechatService.get(id);
			}else {
				userwechat=new Userwechat();
				userwechat.setDelStatus(DelStatus.NODEL);
				userwechat.setGmtCreated(new Date());
			}
			Long userId=optLong("userId",request);
			if (userId!=null){
				userwechat.setUserId(userId);
			}
			String openid=optString("openid",request);
			if (openid!=null){
				userwechat.setOpenid(openid);
			}
			String unionid=optString("unionid",request);
			if (unionid!=null){
				userwechat.setUnionid(unionid);
			}
			String subscribe=optString("subscribe",request);
			if (subscribe!=null){
				userwechat.setSubscribe(subscribe);
			}
			String appid=optString("appid",request);
			if (appid!=null){
				userwechat.setAppid(appid);
			}
			userwechat.setGmtModify(new Date());
			userwechatService.saveOrUpdate(userwechat);
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成");
		}

		/**
		 *批量删除用户微信信息
		*/
		@ResponseBody
		@RequestMapping("delUserwechatByIds")
		public String delUserwechatByIds(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			String ids=getString("ids",request);
			List<Long> idsList=ObjectUtil.getIdListByIds(ids);
			if (idsList==null||idsList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.PARAMERROR, "ids参数错误");
			}
			Map<String, Object> params=new HashMap<String, Object>();
			params.put(Common.DELSTATUS, DelStatus.NODEL);
			params.put(Common.ID, idsList);
			List<Userwechat> userwechatList= userwechatService.getByParams(params);
			if (userwechatList==null||userwechatList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			for (Userwechat userwechat : userwechatList) {
				userwechat.setDelStatus(DelStatus.DEL);
			}
			userwechatService.saveOrUpdateAll(userwechatList);
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成");
		}

		/**
		 * 分页查询用户微信信息
		*/
		@ResponseBody
		@RequestMapping("getUserwechat")
		public String getUserwechat(ModelMap modelMap,HttpServletRequest request) throws BusinessException,ParseException{
			Integer pageIndex=optInteger("pageIndex",request,1);
			Integer pageNum=optInteger("pageNum",request,10);
			String des=optString("des",request,Common.DESC);
			String pms=optString("pms",request);
			Date start=optDate("start",request,"yyyy-MM-dd HH:mm:ss");
			Date end=optDate("end",request,"yyyy-MM-dd HH:mm:ss");
			Map<String, Object> prams=new HashMap<String, Object>();
			prams.put(Common.DELSTATUS, DelStatus.NODEL);
			Map<String, Object> sortPram=new HashMap<String, Object>();
			sortPram.put(Common.ID,des);
			Map<String, Object> searchPram=null;
			if (pms!=null) {
				searchPram=new HashMap<String, Object>();
			}
			Map<String, Object> startTime=null;
			if(start!=null){
				startTime=new HashMap<String, Object>();
				startTime.put(Common.GMTCREATED,start);
			}
			Map<String, Object> endTime=null;
			if(end!=null){
				endTime=new HashMap<String, Object>();
				endTime.put(Common.GMTCREATED,end);
			}
			List<Userwechat> userwechatList=userwechatService.getByParamsList(prams, searchPram,sortPram,  (pageIndex-1)*pageNum, pageNum, startTime, endTime);
			if (userwechatList==null||userwechatList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			Integer pageCount=userwechatService.getCountByParamss(prams, searchPram, startTime, endTime);
			modelMap.put(CommonInfoUtil.PAGECOUNT, Page.getCount(pageCount, pageNum));//pageCount  总页数
			modelMap.put(CommonInfoUtil.PAGEINDEX, pageIndex);			//pageIndex     查询页数
			modelMap.put(CommonInfoUtil.ALLCOUINT,pageCount);
			modelMap.put(CommonInfoUtil.DATA, userwechatList);		//jsonObjectList json对象集合
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",modelMap);
		}

		/**
		 *根据id查询用户微信信息
		*/
		@ResponseBody
		@RequestMapping("getUserwechatById")
		public String getUserwechatById(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			Long id=getLong("id",request);
			Userwechat userwechat=userwechatService.get(id);
			if (userwechat==null) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",userwechat);//jsonObject	json对象
		}

		/**
		 * 查询所有用户微信信息
		 * @param modelMap
		 * @param session
		 * @return
		 */
		@ResponseBody
		@RequestMapping("getAllUserwechat")
		public String getAllUserwechat(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			List<Userwechat> userwechatList= userwechatService.getByParams(null);
			if (userwechatList==null||userwechatList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",userwechatList);//jsonObjectList	json对象集合
		}
}
